package com.qdxwx.web.controller.user;

import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.FastJsonUtils;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.user.UserThirdService;
import com.qdxwx.models.enums.UserThirdType;
import com.qdxwx.models.user.UserThird;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class ThirdPartBindController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ThirdPartBindController.class);

    @Autowired
    private UserThirdService userThirdService;

    /**
     * 初始化修改用户信息页面
     */
    @RequestMapping("/threeBind")
    public String threeBind(HttpServletRequest request) {
        try {
            List<UserThird> userThirdList = userThirdService.getListByUserId(LoginUser.getUserId(request));
            //QQ第三方信息
            UserThird userThirdQQ = new UserThird();
            UserThird userThirdWeiXin = new UserThird();
            //循环查出第三方信息
            for (UserThird userThird : userThirdList) {
                if (UserThirdType.QQ.ordinal() == userThird.getType()) {
                    //QQ
                    userThirdQQ = userThird;
                }
                if (UserThirdType.WEIXIN.ordinal() == userThird.getType()) {
                    //微信
                    userThirdWeiXin = userThird;
                }
            }
            request.setAttribute("WEIXIN_LOGIN_IS_OPEN", CacheConstants.WEIXIN_LOGIN_IS_OPEN);
            request.setAttribute("userThirdQQ", userThirdQQ);
            request.setAttribute("userThirdWeiXin", userThirdWeiXin);
            //微信内置浏览器
            request.setAttribute("isWebchatPage", TKRequest.isWxBrowse(request));
        } catch (Exception e) {
            logger.error("threelogin-error", e);
            return this.toErrorView(request, e);
        }
        return getViewPath("ucenter/three-bind");
    }

    /**
     * 第三方绑定解绑操作
     */
    @RequestMapping("/ajax/removeBinding")
    @ResponseBody
    public AjaxResult excludeBunging(@RequestParam("id") int id) {
        try {
            if (ObjectUtils.isNotNull(id)) {
                userThirdService.delete(id);
                return AjaxResult.success("解绑成功！");
            } else {
                return AjaxResult.error("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            logger.error("excludeBunging-error", e);
            return AjaxResult.error("系统繁忙,请稍后再试");
        }
    }

    /**
     * 第三方关联
     */
    @RequestMapping("/binding")
    public String bindingProfile(HttpServletRequest request,
                                 @ModelAttribute("userThirdJson") String userThird) {
        try {
            if (LoginUser.getUserId(request) > 0) {
               // return "redirect:/user/index.html";
            }
            //接收第三方用户信息
            if (TKString.isNotEmpty(userThird)) {
                Map<String, String> userThirdMap = FastJsonUtils.json2StringMap(userThird);
                request.setAttribute("userThird", userThirdMap);
            }
        } catch (Exception e) {
            logger.error("UserController.bindingProfile-error", e);
            toErrorView(request, e);
        }
        return getViewPath("auth/binding-third");
    }
}
